using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using Hotran.UI;
using Hotran.Audio;
using TMPro;

public class UiManager : MonoBehaviour
{
    [SerializeField] Game game;

    [SerializeField] SceneTransition sceneTransition;

    [SerializeField] Button restartButton;
    [SerializeField] Button tipButton;
    [SerializeField] Button closeTipButton;
    [SerializeField] TextMeshProUGUI countDownText;
    [SerializeField] TextMeshProUGUI scoreText;
    [SerializeField] Image tip;
    [SerializeField] Image[] healthPoints;
    [SerializeField] UiPanel tipPanel;
    [SerializeField] CanvasGroup gameScore;
    [SerializeField] CanvasGroup gameOver;

    [SerializeField] AudioClip countDownSound;

    private readonly string[] countDownDigits = { "3", "2", "1", "Start" };

    // Start is called before the first frame update
    void Start()
    {
        HideText(countDownText);
        gameOver.alpha = 0f;
        gameScore.alpha = 0f;

        UpdateScore();

        StartCoroutine(DoCountDown());
    }

    private void OnEnable()
    {
        game.HealthChanged += UpdateHealth;
        game.GameEnded += GameOver;

        restartButton.onClick.AddListener(Restart);
        tipButton.onClick.AddListener(ToggleTip);
        closeTipButton.onClick.AddListener(ToggleTip);
    }

    private void OnDisable()
    {
        game.HealthChanged -= UpdateHealth;
        game.GameEnded -= GameOver;

        restartButton.onClick.RemoveListener(Restart);
        tipButton.onClick.RemoveListener(ToggleTip);
        closeTipButton.onClick.RemoveListener(ToggleTip);
    }

    private void UpdateHealth()
    {
        if (game.Health >= 0)
        {
            var hp = healthPoints[game.Health];
            hp.rectTransform.DOScale(2f, 0.3f);
            hp.DOFade(0f, 0.5f);
        }
    }

    private void UpdateScore() => scoreText.text = game.Score.ToString();

    public void ToggleTip()
    {
        if (!game.Started) return;

        tipPanel.ToggleImmediately();
        game.TogglePause();
    }

    public void ReturnToMenu()
    {
        Time.timeScale = 1f;
        sceneTransition.FadeOut(actionOnFaded: game.ReturnToMenu);
    }

    private void GameOver()
    {
        AudioPlayer.Instance.BgmFadeOut();

        UpdateScore();
        gameOver.blocksRaycasts = true;
        gameOver.DOFade(1f, 0.3f);
        gameScore.DOFade(1f, 0.5f);
    }

    private void Restart()
    {
        Time.timeScale = 1f;
        sceneTransition.FadeOut(actionOnFaded: game.Restart);
    }

    private IEnumerator DoCountDown()
    {
        var waitForHalfSecond = new WaitForSeconds(1f);
        yield return new WaitForSeconds(0.5f);

        AudioPlayer.Instance.PlaySound(countDownSound);
        foreach (var digit in countDownDigits)
        {
            HideText(countDownText);
            countDownText.rectTransform.localScale = Vector3.one * 4f;

            countDownText.text = digit;
            countDownText.DOFade(1f, 0.4f);
            countDownText.rectTransform.DOScale(1f, 0.4f);

            if (digit != countDownDigits[^1])
                yield return waitForHalfSecond;
            else yield return new WaitForSeconds(0.4f);
        }
        countDownText.rectTransform.DOScale(new Vector3(2f, 0f, 1f), 0.2f);
        countDownText.DOFade(0f, 0.2f);

        tip.rectTransform.DOScale(new Vector3(1.5f, 0f, 1f), 0.2f);
        tip.DOFade(0f, 0.2f);


        //tip.DOFade(0f, 0.3f);
        //tip.rectTransform.DOScale(0f, 0.3f);
        //tip.rectTransform.DOMove(tipButton.transform.position, 0.3f);

        game.StartGame();
    }

    private void HideText(TextMeshProUGUI text)
    {
        // ������Ϊ͸��
        var color = text.color;
        color.a = 0f;
        text.color = color;
    }
}
